package com.Register.controller.admin;

import com.Register.dto.ActivityPageQueryDTO;
import com.Register.dto.ActivityUpdateDTO;
import com.Register.dto.CreateActivityDTO;
import com.Register.result.PageResult;
import com.Register.result.Result;
import com.Register.service.VolunteerActivityService;
import com.Register.vo.SearchActivityByIdVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;

@Tag(name="管理员端活动接口")
@RestController("WebAdminActivityController")
@RequestMapping("/web/admin/activity")
@Slf4j
public class VolunteerActivityController {

    @Resource
    private VolunteerActivityService volunteerActivityService;

    /**
     * 管理员创建
     * @param createActivityDTO
     */
    @Operation(summary = "管理员创建志愿活动")
    @PostMapping("/create")
    public Result<String> CreateVolunteerActivity(@RequestBody CreateActivityDTO createActivityDTO) throws JsonProcessingException {
        //调用service方法
        volunteerActivityService.CreateActivity(createActivityDTO);
        return Result.success("创建志愿活动成功");
    }

    /**
     *
     * 根据id查询活动
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询活动")
    @GetMapping("/activity/{id}")
    public Result<SearchActivityByIdVO> searchActivityById(@PathVariable("id") Long id){
        return Result.success(volunteerActivityService.searchActivityById(id));
    }

    /**
     * 分页查询活动
     * @param activityPageQueryDTo
     * @return
     */
    @Operation(summary = "分页查询活动")
    @GetMapping("/activityPage")
    public Result<PageResult> VolunteerActivityPageQuery(@ParameterObject ActivityPageQueryDTO activityPageQueryDTo) throws JsonProcessingException {
        log.info("进行活动分页查询，查询参数为:{}",activityPageQueryDTo);
        return Result.success(volunteerActivityService.ActivityPageQuery(activityPageQueryDTo));
    }

    @Operation(summary = "管理员更新活动")
    @PutMapping("/update")
    public Result<String> UpdateActivity(@RequestBody ActivityUpdateDTO activityUpdateDTO) throws IOException, ParseException {
        log.info("活动信息修改:{}",activityUpdateDTO);
        return volunteerActivityService.UpdateActivity(activityUpdateDTO);
    }

    @GetMapping("/export/{id}")
    @Operation(summary = "导出参加该活动的人员名单")
    public void exportActivityData(@PathVariable("id") Long activityId, HttpServletResponse httpServletResponse) throws IOException {
        volunteerActivityService.exportUserInfoToExcel(activityId,httpServletResponse);
    }
}
